#include<climits>

class Solution {
public:
    int myAtoi(string str) {
        long long  result = 0;
        int i = 0;
        int temp,d;
        while(str[i]==' ')
            i++;
        d=1;
        if(str[i]=='+') {d=1;i++;} else
        if(str[i]=='-') {d=-1;i++;}
        while(str[i]=='0')
            i++;
        if(str[i]>'9' or str[i]<'0') return 0;
        for(; i < str.length(); i++)
        {
            if(str[i]>'9' or str[i]<'0') break;

            temp = str[i]-'0';
            result = temp+result*10;
            if(result*d >= INT_MAX ) return INT_MAX;
            if(result*d <= INT_MIN ) return INT_MIN;
        }
        return result*d;
    }
};
